package edu.arizona.sista.learn.language.model;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import edu.arizona.sista.learn.activity.model.ActivityInstance;
import edu.arizona.sista.learn.activity.model.PredicateInterval;

public class StoryMaker {

	public static void main(String args[]) {

		File fluentsFile = new File("data/input/fluents/newspaper-fluents.lisp");
		List<ActivityInstance> instances = ActivityInstance.load(fluentsFile);
		List<PredicateInterval> intervals = new ArrayList<PredicateInterval>();
		ActivityInstance inst = instances.get(0);

		for (String key : inst.getIntervalsMap().keySet()) {
			intervals.addAll(inst.getIntervals(key));
		}

		Collections.sort(intervals, PredicateInterval.esf);
		HashMap<String, LinkedList<PredicateInterval>> myMap = new HashMap<String, LinkedList<PredicateInterval>>();
		int count = 0;
		for (int i = 0; i < intervals.size(); i++) {
			if (!myMap.keySet().contains(intervals.get(i).getName())) {
				LinkedList<PredicateInterval> PI = new LinkedList<PredicateInterval>();
				PI.add(intervals.get(i));
				myMap.put(intervals.get(i).getName(), PI);
				count += PI.size();
			} else {
				myMap.get(intervals.get(i).getName()).add(intervals.get(i));
				count += 1;
			}
		}
		System.out.println(count);
		HashMap<String, LinkedList<PredicateInterval>> myNewMap = merge(myMap);
		HashMap<String, LinkedList<PredicateInterval>> myNewNewMap = tooShort(myNewMap);
		HashMap<String, LinkedList<PredicateInterval>> myNewNewNewMap = hierarchey(myNewNewMap);
		ActivityInstance cleaned = new ActivityInstance();
		for (String s : myNewNewNewMap.keySet()) {
			LinkedList<PredicateInterval> temp = new LinkedList<PredicateInterval>(
					myNewNewNewMap.get(s));
			cleaned.addIntervals(temp);
		}
	}

	public static HashMap<String, LinkedList<PredicateInterval>> merge(
			HashMap<String, LinkedList<PredicateInterval>> map) {
		int count = 0;
		for (String s : map.keySet()) {
			LinkedList<PredicateInterval> temp = new LinkedList<PredicateInterval>(
					map.get(s));
			map.remove(temp);
			int length = temp.size();
			for (int i = 0; i < length + 1; i++) {
				if (temp.size() > 1) {
					PredicateInterval first = temp.pop();
					PredicateInterval second = temp.pop();
					if ((second.start - first.end) < 45) {
						first.end = second.end;
						temp.push(first);
					}
				}
			}
			if (temp.size() > 1) {
				PredicateInterval first = temp.pop();
				PredicateInterval second = temp.pop();
				while (temp.size() != 1 && first.end > second.end) {
					second = temp.pop();
				}
				temp.push(second);
				temp.push(first);
			}
			if (temp.size() > 0) {

				count += temp.size();
				map.put(temp.getFirst().getName(), temp);
			}

		}
		// System.out.println(map);
		return map;
	}

	private static HashMap<String, LinkedList<PredicateInterval>> tooShort(
			HashMap<String, LinkedList<PredicateInterval>> map) {
		HashMap<String, LinkedList<PredicateInterval>> newMap = new HashMap<String, LinkedList<PredicateInterval>>();
		int count = 0;
		for (String s : map.keySet()) {
			LinkedList<PredicateInterval> temp = new LinkedList<PredicateInterval>(
					map.get(s));
			map.remove(temp);
			int length = temp.size();
			for (int i = 0; i < length; i++) {
				LinkedList<PredicateInterval> newTemp = new LinkedList<PredicateInterval>();
				PredicateInterval check = temp.pop();
				if ((check.end - check.start) > 30) {
					System.out.println(check);
					newTemp.add(check);
				}
				if (newTemp.size() > 0) {
					count += newTemp.size();
					newMap.put(newTemp.getFirst().getName(), newTemp);
				}
			}
		}
		// System.out.println(count);
		if (newMap.size() > 0) {
			map = newMap;
		}
		return map;
	}

	private static HashMap<String, LinkedList<PredicateInterval>> hierarchey(
			HashMap<String, LinkedList<PredicateInterval>> map) {
		HashMap<String, LinkedList<PredicateInterval>> myMap = new HashMap<String, LinkedList<PredicateInterval>>();

		for (String s : map.keySet()) {
			LinkedList<PredicateInterval> temp = new LinkedList<PredicateInterval>(
					map.get(s));// I have temp I want to make a new linked list
								// based on the agent then add it to a new hash
								// map then use that hash map to compare verbs
			PredicateInterval PI = temp.pop();
			LinkedList<PredicateInterval> myTemp = new LinkedList<PredicateInterval>();
			myTemp.add(PI);
			if (!myMap.containsKey(PI.getPredicate().getArg(0).toString())) {
				myMap.put(PI.getPredicate().getArg(0).toString(), myTemp);
			} else {
				myMap.get(PI.getPredicate().getArg(0).toString()).addLast(PI);
			}
			// we have checked that this works so far now we need to put in the
			// check
		}int count = 0;
		for (String s : myMap.keySet()) {
			LinkedList<PredicateInterval> temp = new LinkedList<PredicateInterval>(
					myMap.get(s));
			count+=temp.size();
			System.out.println(temp + "\n" + count);
		}
		if (myMap.size() > 0) {
			map = myMap;
		}
		return map;
	}
}
